package com.uenpay.im.panel;

import android.content.Context;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.qyx.android.message.type.PacketType;
import com.uenpay.im.R;
import com.uenpay.im.panel.more.IMoreOnClickListener;
import com.uenpay.im.panel.more.MoreAdapter;
import com.uenpay.im.panel.more.MoreEntity;

import java.util.ArrayList;

/**
 * @author SL
 * @created 2016-4-12 下午3:52:43
 * @copyRight http://www.qiyunxin.com
 * @function 面板中更多数据
 */
public class PanelMoreManager {

	/**
	 * 下标layout
	 */
	private LinearLayout moreIndexGroup;
	/**
	 * 数据ViewPage
	 */
	private ViewPager moreViewPager;
	private Context mContext;
	private MoreAdapter moreAdapter = null;

	/**
	 * 更多按钮管理
	 * 
	 * @param _moreIndexGroup
	 *            下标layout
	 * @param _moreViewPager
	 *            数据ViewPage
	 * @param _Context
	 */
	public PanelMoreManager(LinearLayout _moreIndexGroup,
			ViewPager _moreViewPager, Context _Context) {
		moreIndexGroup = _moreIndexGroup;
		moreViewPager = _moreViewPager;
		mContext = _Context;
	}

	/**
	 * 获取更多显示的数据
	 * 
	 * @param chatType
	 *            1：私聊2：群聊
	 * @return
	 */
	public static ArrayList<MoreEntity> getPanelMoreData(int chatType) {

		ArrayList<MoreEntity> arrayList = new ArrayList<MoreEntity>();
		MoreEntity moreEntity1 = new MoreEntity(
				R.drawable.action_take_photo_selector,
				R.string.action_take_photo, 1);
		MoreEntity moreEntity2 = new MoreEntity(
				R.drawable.action_pick_image_selector,
				R.string.action_pick_image, 2);
		MoreEntity moreEntity3 = new MoreEntity(
				R.drawable.action_location_selector, R.string.action_location,
				3);
		MoreEntity moreEntity4 = new MoreEntity(
				R.drawable.action_file_selector, R.string.action_file, 4);
		MoreEntity moreEntity5 = new MoreEntity(
				R.drawable.action_crad_selector, R.string.action_crad, 5);
		MoreEntity moreEntity6 = new MoreEntity(R.drawable.action_red_selector,
				R.string.red, 6);
		MoreEntity moreEntity7 = new MoreEntity(
				R.drawable.action_video_chat_selector, R.string.video_chat, 7);
		MoreEntity moreEntity8 = new MoreEntity(
				R.drawable.action_crowdfund_selector, R.string.crowdfund, 8);
		MoreEntity moreEntity9 = new MoreEntity(
				R.drawable.action_transfer_selector, R.string.transfer, 9);
		MoreEntity moreEntity10 = new MoreEntity(
				R.drawable.action_video_selector, R.string.small_video, 10);
		arrayList.add(moreEntity1);
		arrayList.add(moreEntity2);
		arrayList.add(moreEntity3);
		arrayList.add(moreEntity4);
		arrayList.add(moreEntity5);
		arrayList.add(moreEntity6);
		arrayList.add(moreEntity10);
		if (chatType == PacketType.PERSONAL) {
//			msg_list.add(moreEntity7);
			arrayList.add(moreEntity9);
		}
		if (chatType == PacketType.GROUP) {
			arrayList.add(moreEntity8);
		}
		return arrayList;
	}

	/**
	 * 初始化更多数据
	 */
	public void initMoreAdapter(ArrayList<MoreEntity> _ArrayList,
			IMoreOnClickListener mIMoreOnClickListener) {
		moreAdapter = new MoreAdapter(mContext, _ArrayList,
				mIMoreOnClickListener);
		moreViewPager.setAdapter(moreAdapter);
		moreViewPager.setOnPageChangeListener(new OnPageChangeListener() {

			@Override
			public void onPageSelected(int arg0) {
				showMoreIndexMark(arg0);
			}

			@Override
			public void onPageScrolled(int arg0, float arg1, int arg2) {
			}

			@Override
			public void onPageScrollStateChanged(int arg0) {
			}
		});
		showMoreIndexMark(0);
	}

	/**
	 * 显示更多标点
	 * 
	 * @param index
	 */
	private void showMoreIndexMark(int index) {
		moreIndexGroup.removeAllViews();
		for (int i = 0, max = moreAdapter.getCount(); i < max; i++) {
			ImageView image = new ImageView(mContext);
			LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
					LinearLayout.LayoutParams.WRAP_CONTENT,
					LinearLayout.LayoutParams.WRAP_CONTENT);
			lp.leftMargin = 10;
			lp.rightMargin = 10;
			lp.topMargin = 5;
			lp.bottomMargin = 5;
			if (index == i) {
				image.setImageResource(R.drawable.point_selected);
			} else {
				image.setImageResource(R.drawable.point);
			}
			image.setLayoutParams(lp);
			moreIndexGroup.addView(image);
		}
		if (moreIndexGroup.getChildCount() < 2) {
			moreIndexGroup.setVisibility(View.GONE);
		}
	}

}
